let mongoose = require('mongoose');
let ObjectId = mongoose.Schema.Types.ObjectId;
const conn = mongoose.createConnection('mongodb://localhost:27017/xx',{ useNewUrlParser: true });
let UserSchema = new mongoose.Schema({
    username:{type:String,required:true},
    password:String,
    age:Number,
    createAt:{type:Date,default:Date.now}
});
//在类上还是实例 上扩展，关键要看这个操作是针对的集合还是单个实例 
//扩展类上的静态方法
UserSchema.statics.login = function(username,password){
   return  this.findOne({username,password});
}
//扩展实体或者实例 的方法 Entity
UserSchema.methods.login = function(){
  return this.model('User').findOne({username:this.username,password:this.password});
}
UserSchema.methods.isGrowUp = function(){
    return this.age>18;
  }
let User = conn.model('User',UserSchema);
/**
 * 如何扩展静态和实例方法

(async function login(username,password){
    await User.create({username:'1',password:'1'});
})();
(async function login(username,password){
     User.findOne({username,password},function(err,doc){
    }); 
    // {code:0} {code:1}
    let result = await User.login(username,password);
    console.log('result',result);

})('1','1');
*/
(async function login(username,password){
    let user = new User({username,password});
    let result =await user.login();
    console.log('result',result);
})('1','1');